package com.ruoyi.jihuo.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.jihuo.domain.JhSysTrafficRecord;
import com.ruoyi.jihuo.domain.JhTmAccount;
import com.ruoyi.jihuo.domain.search.JhSysTrafficRecordSearch;
import com.ruoyi.jihuo.mapper.JhSysTrafficRecordMapper;
import com.ruoyi.jihuo.mapper.JhTmAccountMapper;
import com.ruoyi.jihuo.service.IJhSysTrafficRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IJhSysTrafficRecordServiceImpl implements IJhSysTrafficRecordService {

    private static final Logger log = LoggerFactory.getLogger(IJhSysTrafficRecordServiceImpl.class);

    @Autowired
    private JhSysTrafficRecordMapper jhSysTrafficRecordMapper;

    @Autowired
    private JhTmAccountMapper jhTmAccountMapper;

    @Override
    public List<JhSysTrafficRecord> selectJhSysTrafficRecordList(JhSysTrafficRecordSearch sysTrafficRecord) {
        return jhSysTrafficRecordMapper.selectJhSysTrafficRecordList(sysTrafficRecord);
    }

    @Override
    public int insertJhSysTrafficRecord(JhSysTrafficRecord sysTrafficRecord) {
        sysTrafficRecord.setJhTraffic(IdUtils.randomUUID());
        sysTrafficRecord.setJhDeleted("0");
        return jhSysTrafficRecordMapper.insert(sysTrafficRecord);
    }

    @Override
    public JhSysTrafficRecord selectJhSysTrafficRecordById(Long id) {
        return jhSysTrafficRecordMapper.selectJhSysTrafficRecordById(id);
    }

    @Override
    public int updateJhSysTrafficRecord(JhSysTrafficRecord sysTrafficRecord) {
        return jhSysTrafficRecordMapper.updateById(sysTrafficRecord);
    }

    // 流量充值，里面至少有 jhTraffic ; jhSellClient ;  jhTmAccountId ;jhId
    // todo 暂无校验
    @Override
    public String rechargeTraffic(JhSysTrafficRecord jhSysTrafficRecord) {

        //1、充值前先校验该流量包是否已经售卖过
        List<JhSysTrafficRecord> list = jhSysTrafficRecordMapper.selectJhSysTrafficRecordListByTraffic(jhSysTrafficRecord.getJhTraffic());
        if(list != null && list.size() > 0){

            JhSysTrafficRecord trafficRecord = list.get(0);
            if("0".equals(trafficRecord.getJhSellStatus())){
//                return 8;//流量包已使用过
                return "流量包已使用过";//流量包已使用过
            } else {
                //2 、更新流量包的售卖信息
                jhSysTrafficRecord.setJhSellClient(SecurityUtils.getUserId());
                jhSysTrafficRecord.setJhSellStatus("0");
                int rows = jhSysTrafficRecordMapper.rechargeTraffic(jhSysTrafficRecord);
                //3 、流量串售卖成功后，更新店铺加油包数量
                if(rows > 0){
                    JhTmAccount jhTmAccount = jhTmAccountMapper.selectJhTmAccountByJhId(jhSysTrafficRecord.getJhTmAccountId());
                    if(jhTmAccount != null){

                        if(list != null && list.size() > 0){
                            jhTmAccount.setJhTrafficNum(jhTmAccount.getJhTrafficNum() + list.get(0).getJhNum());
                            jhTmAccountMapper.updateById(jhTmAccount);
                            return "1";
                        } else {
//                            return 2;//流量加密串记录表更新成功，但是店铺加油包更新失败
                            return "代码 2：充值失败";
                        }
                    }else {
//                        return 2;//流量加密串记录表更新成功，但是店铺加油包更新失败，没查到店铺，该情况不大可能发生
                        return "代码 2：充值失败";
                    }
                } else{
                    return "代码 0：充值失败";
                }
            }
        } else {
//            return 9;//流量包输入错误
            return "流量兑换码输入错误";//流量包输入错误
        }
    }

    @Override
    public int enableJhSysTrafficRecord(Long id) {
        JhSysTrafficRecord jhSysTrafficRecord = jhSysTrafficRecordMapper.selectJhSysTrafficRecordById(id);
        jhSysTrafficRecord.setJhEnabled("1");
        return jhSysTrafficRecordMapper.updateById(jhSysTrafficRecord);
    }

    @Override
    public int disableJhSysTrafficRecord(Long id) {
        JhSysTrafficRecord jhSysTrafficRecord = jhSysTrafficRecordMapper.selectJhSysTrafficRecordById(id);
        jhSysTrafficRecord.setJhEnabled("0");
        return jhSysTrafficRecordMapper.updateById(jhSysTrafficRecord);
    }

    @Override
    public int deleteJhSysTrafficRecordById(Long id) {
        JhSysTrafficRecord jhSysTrafficRecord = jhSysTrafficRecordMapper.selectJhSysTrafficRecordById(id);
        jhSysTrafficRecord.setJhDeleted("1");
        return jhSysTrafficRecordMapper.deleteJhSysTrafficRecordById(id);
    }

    @Override
    public List<JhSysTrafficRecord> getCurrentUserTrafficRecord() {
        return jhSysTrafficRecordMapper.selectJhSysTrafficRecordListBySellClient(SecurityUtils.getUserId());
    }
}
